/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJDialog.java
 *
 * Created on Aug 20, 2011, 3:16:56 PM
 */

package tuitionmanagement.gui;

import javax.swing.JOptionPane;
import tuitionmanagement.DAO.AccountDAO;
import tuitionmanagement.entity.Account;

/**
 *
 * @author Home
 */
public class LoginDialog extends javax.swing.JDialog {
    AccountDAO daoAccount=new AccountDAO();
public boolean loginSuccessed=false;
    /** Creates new form NewJDialog */
    public LoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        lblUsername1 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        lblPassword1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnLoginOk = new javax.swing.JButton();
        btnLoginCancel = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        lblUsername1.setText("Usename:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 15, 5, 5);
        getContentPane().add(lblUsername1, gridBagConstraints);

        txtUsername.setMinimumSize(new java.awt.Dimension(150, 20));
        txtUsername.setPreferredSize(new java.awt.Dimension(150, 20));
        txtUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUsernameActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 5, 16);
        getContentPane().add(txtUsername, gridBagConstraints);

        lblPassword1.setText("Old password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(1, 15, 1, 5);
        getContentPane().add(lblPassword1, gridBagConstraints);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Login");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 11);
        getContentPane().add(jLabel1, gridBagConstraints);

        btnLoginOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/check2.png"))); // NOI18N
        btnLoginOk.setText("Ok");
        btnLoginOk.setMinimumSize(new java.awt.Dimension(85, 25));
        btnLoginOk.setPreferredSize(new java.awt.Dimension(85, 25));
        btnLoginOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginOkActionPerformed(evt);
            }
        });
        jPanel2.add(btnLoginOk);

        btnLoginCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnLoginCancel.setText("Cancel");
        btnLoginCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnLoginCancel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(12, 10, 0, 11);
        getContentPane().add(jPanel2, gridBagConstraints);

        txtPassword.setMinimumSize(new java.awt.Dimension(150, 20));
        txtPassword.setPreferredSize(new java.awt.Dimension(150, 20));
        txtPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
        getContentPane().add(txtPassword, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_btnLoginCancelActionPerformed

    private void btnLoginOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginOkActionPerformed
        // TODO add your handling code here:
       if(txtUsername.getText().trim().equals("") || txtPassword.getText().trim().equals(""))
       {
        JOptionPane.showMessageDialog(this, "Username and password cannot be left blank");
        return;
       }
        Account acc=new Account();
         acc.setUsername(txtUsername.getText());
        acc.setPassword(new String(txtPassword.getPassword()));
      if(daoAccount.authencicate(acc)){
       JOptionPane.showMessageDialog(this, "Loged in successfully");
       MainWindow.CurrentAccount=acc;
       this.dispose();
      }else JOptionPane.showMessageDialog(this,"Invalid username or password. Please try again");

    }//GEN-LAST:event_btnLoginOkActionPerformed

    private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActionPerformed
        // TODO add your handling code here:
        btnLoginOkActionPerformed(evt);
    }//GEN-LAST:event_txtPasswordActionPerformed

    private void txtUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUsernameActionPerformed
        // TODO add your handling code here:
        btnLoginOkActionPerformed(evt);
    }//GEN-LAST:event_txtUsernameActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                LoginDialog dialog = new LoginDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLoginCancel;
    private javax.swing.JButton btnLoginOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblPassword1;
    private javax.swing.JLabel lblUsername1;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

}
